﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using System.Data.Services.Client;

namespace WCFServiceWebRole1
{
    public class FilesDataSource
    {
        private FilesDataServiceContext _ServiceContext = null;

        public FilesDataSource(CloudStorageAccount storageAccount)
        {
            // var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
            _ServiceContext = new FilesDataServiceContext(storageAccount.TableEndpoint.ToString(), storageAccount.Credentials);

            // Create the tables
            // In this case, just a single table.  
            storageAccount.CreateCloudTableClient().CreateTableIfNotExist(FilesDataServiceContext.FilesTableName);
        }

        public IEnumerable<FilesDataModel> Select()
        {
            var results = (from c in _ServiceContext.FilesTable
                           select c).Take(100);

            var query = results.AsTableServiceQuery<FilesDataModel>();
            var queryResults = query.Execute();

            return queryResults;
        }
 

        public void Insert(FilesDataModel item)
        {
            throw new NotImplementedException();
        }

        public FilesDataModel getByGUID(string p)
        {
            var results = (from c in _ServiceContext.FilesTable
                           select c).Take(1);

            var query = results.AsTableServiceQuery<FilesDataModel>();
            FilesDataModel queryResults = query.Execute().First();

            return queryResults;
        }
    }
}
